package cn.assumejson.springframework.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author Mr. Li
 * @version 1.0
 * @since 1.0 2021/11/30 10:48
 */
public class MutablePropertyValues implements PropertyValues, Serializable {

    private List<PropertyValue> propertyValueList;

    public MutablePropertyValues() {
        this.propertyValueList = new ArrayList<>(0);
    }

    public MutablePropertyValues(PropertyValues original) {
        if (original != null) {
            PropertyValue[] propertyValues = original.getPropertyValues();
            this.propertyValueList = Arrays.asList(getPropertyValues());
        }
    }

    @Override
    public void addPropertyValue(PropertyValue pv) {
        this.propertyValueList.add(pv);
    }

    @Override
    public PropertyValue[] getPropertyValues() {
        return this.propertyValueList.toArray(new PropertyValue[0]);
    }

    @Override
    public PropertyValue getPropertyValue(String propertyName) {
        return propertyValueList.stream()
                .filter(propertyValue -> propertyValue.getName().equals(propertyName))
                .findFirst()
                .get();
    }


    @Override
    public boolean contains(String propertyName) {
        return propertyValueList.stream().anyMatch(propertyValue -> propertyValue.getName().equals(propertyName));
    }
}
